package tengxun;

import common.utils.ListNode;

public class Main1 {



    public static void main(String[] args) {

    }

    public ListNode[] solve (int m, ListNode a) {
        // write code here
        if (a == null){
            return null;
        }
        ListNode[] res = new ListNode[m];
        ListNode[] tails = new ListNode[m];
        ListNode head = new ListNode(0);
        head.next = a;
        ListNode cur = a;
        ListNode pre;
        int count = 0;
        while (cur != null){
            int index = cur.val%m;
            if (res[index] != null){
                ListNode tail = tails[index];
                tail.next = cur;
                tails[index] = cur;

            } else {
                res[index] = cur;
                tails[index] = cur;
            }
            pre = cur;
            cur = cur.next;
            pre.next = null;
        }
        return res;
    }
}
/*class Solution {
    public:
    *//**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param m int整型
     * @param a ListNode类 指向彩带的起点，val表示当前节点的val，next指向下一个节点
     * @return ListNode类vector
     *//*
    vector<ListNode*> solve(int m, ListNode* a) {
        vector<ListNode*> res;
        vector<ListNode*> tail;
        if(m==0)
            return res;
        for(int i=0;i<m;++i){
            ListNode* node = new ListNode(-1);
            res.push_back(node);
            tail.push_back(node);
        }
        while(a){
            int x = a->val%m;
            ListNode* node = tail[x];
            ListNode* next = a->next;
            node->next = a;
            node=node->next;
            tail[x] = node;
            node->next = nullptr;
            a = next;
        }
        for(int i=0;i<m;++i){
            ListNode* node = new ListNode(-1);
            res[i] = res[i]->next;
        }
        return res;
    }
};*/
